package com.yz.sharkim.exception;

import com.yz.sharkim.util.ConvertUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

public abstract class AbstractHttpException extends RuntimeException{

    private final HttpResponseStatus status;

    private final String content;

    public AbstractHttpException(HttpResponseStatus status, String content) {
        this.status = status;
        this.content = content;
    }

    public ChannelFuture sendException(ChannelHandlerContext context) {
        return context.channel().writeAndFlush(
                new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                        status, Unpooled.copiedBuffer(content, ConvertUtil.DEFAULT_CHARSET))
        );
    }
}
